package com.zhouxiaoge.json;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 周小哥  gqzmy@163.com 2022/8/14 10:08
 */

public class GsonTest {

    private Employee employeeBuilder() {
        Employee employee = new Employee();
        employee.setId(1L);
        employee.setName("zhouxiaoge");
        employee.setAge(18);
        employee.setEmail("gqzmy@163.com");
        return employee;
    }

    private Address addressBuilder(String postfix) {
        Address address = new Address();

        address.setCountry("中国");
        address.setStreet("河南省周口市" + postfix);
        return address;
    }

    /**
     * 对象转 JSON
     */
    @Test
    public void object2Json() {
        Employee employee = employeeBuilder();
        List<Address> addressList = new ArrayList<>();
        Address address1 = addressBuilder("1");
        Address address2 = addressBuilder("2");
        addressList.add(address1);
        addressList.add(address2);
        employee.setAddressList(addressList);
        Address[] addresses = new Address[]{addressBuilder("3"), addressBuilder("4")};
        employee.setAddresses(addresses);

        String s = GsonUtils.gsonBuilder().toJson(employee);
        System.out.println(s);
        System.out.println("---------------------------------------");
        String s1 = GsonUtils.gson().toJson(employee);
        System.out.println(s1);
    }

    /**
     * JSON 转对象
     */
    @Test
    public void json2Object() {
        String json = """
                {
                   "id": 1,
                   "name": "zhouxiaoge",
                   "age": 18,
                   "email": "gqzmy@163.com",
                   "addresses": [
                     {
                       "country": "中国",
                       "street": "河南省周口市3"
                     },
                     {
                       "country": "中国",
                       "street": "河南省周口市4"
                     }
                   ],
                   "addressList": [
                     {
                       "country": "中国",
                       "street": "河南省周口市1"
                     },
                     {
                       "country": "中国",
                       "street": "河南省周口市2"
                     }
                   ]
                 }""";
        Employee employee = GsonUtils.gson().fromJson(json, Employee.class);
        System.out.println(employee.toString());
    }

    @Test
    public void jsonArray2Object() {
        String jsonArray = """
                [
                    {
                        "country":"中国",
                        "street":"河南省周口市淮阳区"
                    },
                    {
                        "country":"中国",
                        "street":"河南省周口市"
                    }
                ]""";

        Gson gson = GsonUtils.gsonBuilder();

        Address[] addresses = gson.fromJson(jsonArray, Address[].class);
        for (Address address : addresses) {
            System.out.println(address);
        }
        System.out.println("-------------------------------------");
        Type type = new TypeToken<List<Address>>() {
        }.getType();
        List<Address> addressList = gson.fromJson(jsonArray, type);
        for (Address address : addressList) {
            System.out.println(address);
        }
    }

    @Test
    public void nullTest() {
        Employee employee = new Employee();
        employee.setId(1L);
        String s = GsonUtils.gsonBuilder().toJson(employee);
        System.out.println(s);
    }

    @Test
    public void userTest() {
        User user = new User();
        user.setId(1L);
        user.setName("zhouxiaoge");
        user.setAge(18);
        user.setEmail("gqzmy@163.com");
        user.setDeath(true);
        Gson gson = GsonUtils.gsonBuilder();
        String s = gson.toJson(user);
        System.out.println(s);
        System.out.println("---------------------------------------");
        String json = """
                {
                  "id": 1,
                  "name": "zhouxiaoge",
                  "age": 18,
                  "death": "活着",
                  "mail": "gqzmy@163.com",
                  "email": "gqzmy@qq.com",
                  "emailAddress": "gqzmy@outlook.com"
                }""";
        User user1 = gson.fromJson(json, User.class);
        System.out.println(user1);
    }
}
